package com.xps.and.yuntong.Ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xps.and.yuntong.Entity.ShouZhi;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by administrator on 2017-12-14.
 */

public class MyVolletActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_fanhui, iv_right;
    private TextView tv_middle;
    private LinearLayout ll_chongzhi, ll_tixian;
    private ListView lv_shouzhi;


    private View loadMoreView;
    private LinearLayout loadMoreButton;
    private TextView tv_chakan1;
    private ImageView iv_chakan1;
    private Handler handler = new Handler();
    private int position = 0;

    private MenuAdapter menuAdapter;
    private ArrayList<ShouZhi> shouZhis = new ArrayList<>();
    private ArrayList<ShouZhi> shouZhis1 = new ArrayList<>();
    @Override
    protected int getLayout() {
        return R.layout.activity_my_vollet;
    }

    @Override
    protected void setData() {
        loadMoreView = getLayoutInflater().inflate(R.layout.load_more_button, null);
        loadMoreButton = (LinearLayout) loadMoreView.findViewById(R.id.loadMoreButton);

        tv_chakan1 = (TextView) loadMoreView.findViewById(R.id.tv_chakan1);
        iv_chakan1 = (ImageView) loadMoreView.findViewById(R.id.iv_chakan1);
        loadMoreButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                tv_chakan1.setText("正在加载中...");   //设置按钮文字
                tv_chakan1.setTextColor(Color.RED);
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        loadMoreData();
                        menuAdapter.notifyDataSetChanged();
                        tv_chakan1.setText("查看更多...");  //恢复按钮文字
                        tv_chakan1.setTextColor(Color.GREEN);
                    }
                }, 2000);

            }
        });

        lv_shouzhi.addFooterView(loadMoreView);    //设置列表底部视图
        //        menuAdapter = new MenuAdapter(MyVolletActivity.this, shouZhis);
//        lv_shouzhi.setAdapter(menuAdapter);

    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
        addShuJu();

    }

    private void initView() {
        iv_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.my_volley);

        ll_chongzhi = (LinearLayout) findViewById(R.id.ll_chongzhi);
        ll_tixian = (LinearLayout) findViewById(R.id.ll_tixian);
        lv_shouzhi = (ListView) findViewById(R.id.lv_shouzhi);



        iv_fanhui.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        ll_chongzhi.setOnClickListener(this);
        ll_tixian.setOnClickListener(this);
    }




    private void addShuJu() {
        ShouZhi list = new ShouZhi();
        list.setDan_hao(88888);
        list.setEdu("888888");
        list.setTime("09-09");
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);
        shouZhis.add(list);


        Log.e("1111",shouZhis.size()+"");
        if (shouZhis.size() >= 10) {
            for (int i = 0; i <= 9; i++) {
                //     position+=9;
                shouZhis1.add(shouZhis.get(i));
            }
            menuAdapter = new MenuAdapter(this, shouZhis1);
            lv_shouzhi.setAdapter(menuAdapter);

        } else {
            menuAdapter = new MenuAdapter(this, shouZhis);
            lv_shouzhi.setAdapter(menuAdapter);

        }

    }


    private void loadMoreData() {
        Log.e("1111","position"+position+"menuAdapter.getCount()"+menuAdapter.getCount());
        position = menuAdapter.getCount();
//        Log.e("1111111111", recipesData.get(99) + "");
        if (position + 10< shouZhis.size()) {
            for (int i = position + 1; i <= position + 10; i++) {

                menuAdapter.addNewsItem(shouZhis.get(i));
            }
        } else {
            for (int i = position + 1; i <= position; i++) {

                menuAdapter.addNewsItem(shouZhis.get(i));
            }
            Toast.makeText(MyVolletActivity.this, "数据已加载完毕", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.iv_fanhui:

                finish();

                break;

            case R.id.iv_right:


                break;

            case R.id.ll_chongzhi:
                Intent intent1 = new Intent(MyVolletActivity.this, ChongzhiActivity.class);
                startActivity(intent1);
                break;

            case R.id.ll_tixian:
                Intent intent2 = new Intent(MyVolletActivity.this, TixianActivity.class);
                startActivity(intent2);

                break;


        }
    }

    public class MenuAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater layoutInflater;
        private List<ShouZhi> data;

        public MenuAdapter(Context context, List data1) {
            this.context = context;
            this.data = data1;
            layoutInflater = layoutInflater.from(context);

        }


        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.vollet_item, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_shouru = (TextView) convertView.findViewById(R.id.tv_shouru);
                viewHolder.tv_shouru1 = (TextView) convertView.findViewById(R.id.tv_shouru1);
                viewHolder.tv_dingdan_hao1 = (TextView) convertView.findViewById(R.id.tv_dingdan_hao1);
                viewHolder.tv_shijian_day = (TextView) convertView.findViewById(R.id.tv_shijian_day);
                viewHolder.tv_shijian_hour = (TextView) convertView.findViewById(R.id.tv_shijian_hour);
                viewHolder.tv_dingdan_state = (TextView) convertView.findViewById(R.id.tv_dingdan_state);
//                viewHolder.tvMenuName.setMovementMethod(ScrollingMovementMethod.getInstance());
                convertView.setTag(viewHolder);


            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

//            viewHolder.tv_shouru.setText(data.get(position).getTitle().trim());

            return convertView;
        }

        /**
         * 添加数据列表项
         *
         * @param newsitem
         */
        public void addNewsItem(ShouZhi newsitem) {
            data.add(newsitem);
        }
    }

    class ViewHolder {

        TextView tv_shouru;
        TextView tv_shouru1;
        TextView tv_dingdan_hao1;
        TextView tv_shijian_day;
        TextView tv_shijian_hour;
        TextView tv_dingdan_state;


    }

}
